package com.letv.ads.view;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.VideoView;

import com.letv.adlib.managers.status.ad.AdStatusManager;
import com.letv.adlib.sdk.types.AdElementMime;
import com.letv.ads.util.LogInfo;

/**
 * 
 */
public class AdVideoView extends VideoView implements View.OnClickListener,
        IAdView {
    private final static String TAG = "AdVideoView";
    /**
     * 广告素材对象
     */
    private boolean mHasPlayed = false;
    private boolean mHasClosed = false;
    private AdStatusManager mAdStatusManager;
    private AdViewOnclickListenr mAdViewOnclickListenr;
    private AdPlayStateListener mAdPlayStateListener;
    private AdMaterialLoadListener mAdMaterialLoadListener;

    public AdVideoView(Context context) {
        super(context);
    }

    public AdVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void showAD(final AdElementMime adInfo) {
        LogInfo.log(TAG, "showAD hasPlayed=" + mHasPlayed);
        if (mHasPlayed)
            return;
        mHasPlayed = true;
        if (adInfo != null) {
            this.setVideoPath(adInfo.localPath);
            this.requestFocus();
            this.start();
            this.setTag(adInfo);
            this.setOnClickListener(this);
            this.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    LogInfo.log(TAG, "onPrepared hasPlayed=" + mHasPlayed);
                    if (null == mAdStatusManager) {
                        mAdStatusManager = new AdStatusManager(adInfo);
                    }
                    if (null != mAdPlayStateListener) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(KEY_STATE, PLAY_STATE_STARTED);
                        mAdPlayStateListener.onADPlayStateChange(bundle);
                    }
                    if (null != mAdMaterialLoadListener) {
                        mAdMaterialLoadListener.onLoadComplete();
                    }
                }
            });

            this.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
                    if (null != mAdPlayStateListener) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(KEY_STATE, PLAY_STATE_ERROR);
                        mAdPlayStateListener.onADPlayStateChange(bundle);
                    }
                    return true;
                }
            });

            this.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    if (null != mAdPlayStateListener) {
                        Bundle bundle = new Bundle();
                        bundle.putInt(KEY_STATE, PLAY_STATE_COMPLETE);
                        mAdPlayStateListener.onADPlayStateChange(bundle);
                    }
                }
            });

        }
    }

    @Override
    public void closeAD() {
        LogInfo.log(TAG, "closeAD mHasClosed:" + mHasClosed);
        if (mHasClosed) {
            return;
        } else {
            mHasClosed = true;
        }
        this.setOnPreparedListener(null);
        this.setOnCompletionListener(null);
        this.setOnErrorListener(null);
        setVisibility(GONE);
        if (null != mAdStatusManager) {
            mAdStatusManager.onAdClosed();
        }
        if (mHasPlayed) {
            this.stopPlayback();
        }

    }

    @Override
    public void setAdOnClickListener(AdViewOnclickListenr adViewOnclickListenr) {
        mAdViewOnclickListenr = adViewOnclickListenr;
    }

    @Override
    public void setAdMaterialLoadListener(
            AdMaterialLoadListener adMaterialLoadListener) {
        mAdMaterialLoadListener = adMaterialLoadListener;

    }

    @Override
    public void setAdPlayStateListener(AdPlayStateListener adPlayStateListener) {
        mAdPlayStateListener = adPlayStateListener;
    }

    @Override
    public void onClick(View v) {
        AdElementMime adInfo = null;
        Object object = v.getTag();
        if (object instanceof AdElementMime) {
            adInfo = (AdElementMime) object;
        }
        if (null != adInfo) {
            if (null != mAdViewOnclickListenr) {
                mAdViewOnclickListenr.onADClick(adInfo);
            }
        }
    }

}
